package com.asiainfo.indb.view;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

/**
 * 带有title的面板
 *
 * @author zym
 * @date 2019-10-17 12:47
 */
public abstract class BaseTitlePanel extends BaseInDbPanel {
    public BaseTitlePanel(InDbPanel parentPanel) {
        super(parentPanel);
    }

    @Override
    public void render() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createTitledBorder(getPanelTitle()));

        // 二级面板，用于腾出边框
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(getPanelBorder());
        add(mainPanel);

        // 渲染主体
        render(mainPanel);

        // 设置当前面板的宽度和高度
        setPreferredSize(new Dimension(getParentPanel().getWidth(), getPanelHeight(mainPanel)));
    }

    /**
     * 获取面板标题
     *
     * @return 标题
     */
    protected abstract String getPanelTitle();

    /**
     * 获取面板边框
     *
     * @return
     */
    protected Border getPanelBorder() {
        return BorderFactory.createEmptyBorder(0, 5, 5, 5);
    }

    /**
     * 渲染面板
     *
     * @param container 容器
     */
    protected abstract void render(JPanel container);
}